//
//  CZWelcomeViewController.swift
//  WeiBO28
//
//  Created by 飞奔的羊 on 16/5/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class CZWelcomeViewController: UIViewController {

	// MARK: ------懒加载控件------
	//北京吐血
	private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
	
	//头像
	private lazy var headImageView: UIImageView = {
	let vi = UIImageView(image: UIImage(named: "avatar_default_big"))
	vi.layer.cornerRadius = 45
		vi.layer.masksToBounds = true
	
	//设置头像
		if let url = CZUserAccountViewModel.shareUserAccount.userAccount?.avatar_large {
			vi.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big")	)
		
		}
		
	return vi
	}()
	//欢迎信息
	
	private lazy var messageLabel: UILabel = {
	let label = UILabel()
		if let name = CZUserAccountViewModel.shareUserAccount.userAccount?.name {
		
		label.text = name + ", 欢迎回来!"
		
		} else {
		label.text = "欢迎回来"
		
		}
		
		
		
	label.font = UIFont.systemFontOfSize(14)
		label.textColor = UIColor.darkGrayColor()
	
		
		
		
	return label
	}()
	//自定义 view
	override func loadView() {
		view = bgImageView
		
	}
	
    override func viewDidLoad() {
        super.viewDidLoad()

		setUpUI()
		
    }
	override func viewDidAppear(animated: Bool) {
		super.viewDidAppear(animated)
		startAnmtion()
	}
	
	//添加子视图
	private func setUpUI() {
	view.addSubview(headImageView)
		view.addSubview(messageLabel)
	
		
		headImageView.snp_makeConstraints { (make) in
			
			make.centerX.equalTo(view)
			make.top.equalTo(view).offset(200)
			make.size.equalTo(CGSize(width: 90, height: 90))
		}
		
		messageLabel.snp_makeConstraints { (make) in
			make.top.equalTo(headImageView.snp_bottom).offset(10)
			
			make.centerX.equalTo(headImageView)
		}
		
		
		
		
	}

 private func startAnmtion() {
	//改变约束
	headImageView.snp_updateConstraints { (make) in
		make.top.equalTo(view).offset(100)
	}
	//usingSpringWithDamping  阻尼 越大弹性效果越小
//	initialSpringVelocity 初始速度
	//枚举类型什么也不传,直接使用中括号[]
	messageLabel.alpha = 0
	UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
		//让子视图重新布局
		self.view.layoutIfNeeded()
		
		
		}) { (_) in
		
			UIView.animateWithDuration(1, animations: { 
				self.messageLabel.alpha = 1
			}, completion: { (_) in
				
				//进入首页
				NSNotificationCenter.defaultCenter().postNotificationName(SwithRootVCNotification, object: nil)
			})
			
			
	}
	
	
	
	
	}
	
	
	
	
	
}









